package com.rc.saas.mini.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateHelper {

    /**
     * 通用日期格式
     */
    public static final String DATE_COMMON_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT_YYYYMMDDHHMM = "yyyyMMddHHmm";

    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    public static final String DATE_FORMAT_HHMMSS = "HH:mm:ss";

    public static final String DATE_FORMAT_YYMMDDHHMMSS = "yyMMddHHmmss";

    public static Date getCurrentDate() {
        Calendar calender = Calendar.getInstance();
        return calender.getTime();
    }

    /**
     * 获取一个日期，该日期是在date的days天之前(days是负数)或之后(days是正数)
     * @param date
     * @param days
     * @return Date
     */
    public static Date getDateByCalculateDays(Date date, int days) {
        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        calender.add(Calendar.DATE, days);
        return calender.getTime();
    }

    /**
     * 获取一个日期，该日期是在date的seconds秒之前(seconds是负数)或之后(seconds是正数)
     * @param date
     * @param days
     * @return Date
     */
    public static Date getDateByCalculateSeconds(Date date, int seconds) {
        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        calender.add(Calendar.SECOND, seconds);
        return calender.getTime();
    }

    /**
     * 把日期按照某个格式转换成字符串
     * @param date
     * @param format
     * @return
     */
    public static String dataToString(Object date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    /**
     * 把字符串按照某个格式转换成日期类型
     * @param strDate
     * @param format
     * @return
     */
    public static Date stringToDate(String strDate, String format) {
        DateFormat df = new SimpleDateFormat(format);
        Date date = null;
        if (strDate != null && !"".equals(strDate)) {
            try {
                date = df.parse((String) strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    public static String getRandomNum() {
        StringBuffer sb = new StringBuffer();
        sb.append(dataToString(new Date(), DATE_FORMAT_YYYYMMDDHHMM));
        sb.append(String.valueOf(Math.round(Math.random() * 8999 + 1000)));
        return sb.toString();
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static int minuteBetween(Date smDate, Date bdate) throws ParseException {
        return (int) (bdate.getTime() - smDate.getTime()) / 1000 / 60;
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = sdf.parse("2017-05-01 10:10:10");
        Date d2 = sdf.parse("2017-05-15 00:00:00");
        System.out.println(daysBetween(d1, d2));
    }

}
